require 'yaml'
require "event_editor/model"
require "dgo/io/json_builder"
module EventEditor
  class IO
    include DGO::IO
    def self.load(map_model)
      start_info = {}
      switches = []
      viriables = []
      charasets_setting = {}
      File.open($res.data_path + "common/switches.yaml", "r") { |f| switches = YAML::load(f.read)}
      File.open($res.data_path + "common/variables.yaml", "r") { |f| viriables = YAML::load(f.read)}
      File.open("#{$res.data_path}common/start.yaml", "r") { |f| start_info = YAML::load(f.read) }
      File.open("#{$res.data_path}common/charasets_setting.yaml", "r") { |f| charasets_setting = YAML::load(f.read) }

      return Model.new(map_model, start_info, switches, viriables, charasets_setting)
    end

    
    def self.save(model)
      self.save_events(model)
      self.save_start_position(model)
      self.save_character_chips_setting(model)
      self.save_switch_data(model)
    end
    
    def self.save_switch_data(model)
      File.open("#{$res.data_path}common/switches.yaml", "w+") do |f|
        f.write(model.switches.to_yaml)
      end
      File.open("#{$res.data_path}common/variables.yaml", "w+") do |f|
        f.write(model.variables.to_yaml)
      end
    end
    
    def self.save_events(model)
      map_id = model.map_model.map_id
      File.open("#{$res.data_path}event/#{map_id}.yaml", "w+") do |f|
        f.write(model.to_yaml)
      end
      
      #JSON Format
      File.open("#{$res.data_path}event/#{map_id}.json", "w+") do |f|
        f.write(JsonBuilder.new.build(model.events))
      end
    end
    
    def self.save_character_chips_setting(model)
      
      File.open("#{$res.data_path}common/charasets_setting.yaml", "w+") do |f|
        f.write(model.character_chipsets_setting.to_yaml)
      end

      #JSON Format
      File.open("#{$res.data_path}common/charasets_setting.json", "w+") do |f|
        f.write(JsonBuilder.new.build(model.character_chipsets_setting))
      end
      
    end
    
    def self.save_start_position(model)
      arr = {}
      arr['start_x'] = model.player_x
      arr['start_y'] = model.player_y
      arr['start_map_id'] = model.player_map_id
      File.open("#{$res.data_path}common/start.yaml", "w+") do |f|
        f.write(arr.to_yaml)
      end

      #JSON Format
      File.open("#{$res.data_path}common/start.json", "w+") do |f|
        f.write(JsonBuilder.new.build(arr))
      end
      
    end
    
    # Get sound effects
    def self.get_SEs
      arr = []
      arr << ["stop", ""]
      Dir::glob("#{$res.se_path}*").each do |path|
        if File.file?(path)
          b = File.basename(path).split(".")[0]
          arr << [b, b]
        end
      end
      return arr
    end
    
    # Get BGMs
    def self.get_BGMs
      arr = []
      arr << ["stop", ""]
      Dir::glob("#{$res.bgm_path}*").each do |path|
        if File.file?(path)
          b = File.basename(path).split(".")[0]
          arr << [b, b]
        end
      end
      return arr
    end
    
    def self.get_character_images
      arr = []
      Dir::glob("#{$res.image_path}game/character/*.png").each do |path|
        if File.file?(path)
          arr << File.basename(path).split(".")[0]
        end
      end
      return arr
    end
    
    def self.get_move_command_names
      arr = []
      Dir::glob("#{$res.data_path}event/move_type/*.yaml").each do |path|
        if File.file?(path)
          arr << File.basename(path).split(".")[0]
        end
      end
      return arr
    end
    
    def self.save_new_event(map_id)
      File.open("#{$res.data_path}event/#{map_id}.yaml", "w+") do |f|
        f.write([].to_yaml)
      end
      
      #JSON Format
      File.open("#{$res.data_path}event/#{map_id}.json", "w+") do |f|
        f.write(JsonBuilder.new.build([]))
      end
    end
  end
end